//
//  EmoticonToolBar.swift
//  emoticonKB
//
//  Created by mac on 16/3/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

//不占用0的tag值
private let baseTag = 1000

@available(iOS 9.0, *)
class EmoticonToolBar: UIStackView {
    
    //使用闭包传递点击事件
    var emoticonBtnClick: ( (index: Int) -> () )?
    
    //记录点击按钮
    var currentBtn: UIButton?

    init () {
        
        super.init(frame: CGRectZero)
        //设置属性 垂直和水平坐标
        axis = .Horizontal
        
        distribution = .FillEqually
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        let packages = EmoticonManager.sharedEmoticonManager.packages
        var index = 0
        
        for p in packages {
        
            var imageName = "compose_emotion_table_mid"
            
            if index == 0 {
            
                imageName = "compose_emotion_table_left"
            } else if index == packages.count - 1 {
            
                imageName = "compose_emotion_table_right"
            }
            
            addSubBtns(p.name ?? "", BackImageName: imageName,type: index)
            
            index++
        }
//
//        
//        addSubBtns("默认", BackImageName: "compose_emotion_table_left", type: 0)
//        addSubBtns("Emoji", BackImageName: "compose_emotion_table_mid", type: 1)
//        addSubBtns("浪小花", BackImageName: "compose_emotion_table_right", type: 2)
    }
    
    private func addSubBtns(title: String,BackImageName: String,type: Int) {
    
        let btn = UIButton()
        btn.setTitle(title, forState: .Normal)
        
        //设置背景图片和颜色
        btn.setBackgroundImage(UIImage(named: BackImageName + "_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: BackImageName + "_selected"), forState: .Selected)
        
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        btn.tag = baseTag + type
        
        //注册点击事件
        btn.addTarget(self, action: "itemsClick:", forControlEvents: .TouchUpInside)
        
        //默认选中第一个按钮
        if type == 0 {
        
            btn.selected = true
            
            currentBtn = btn
        }
        
        //添加子视图
        addArrangedSubview(btn)
    }
    
    @objc private func itemsClick(btn: UIButton) {
    
        //设置选中按钮
        setSelectedBtn(btn)
        
        
        //完成点击事件的回调 (智能提示不行 可以加? 再. 后删除)
        emoticonBtnClick?(index: btn.tag - baseTag)
        
    }
   
    func selectButtonAtIndexPathOfSection(section: Int) {
        
        //根据section获取对应的按钮
        let btn = self.viewWithTag(baseTag + section) as! UIButton
        //设置按钮的选中状态
        setSelectedBtn(btn)
    }
  
    private func setSelectedBtn(btn: UIButton) {
    
        //取消上一次点击按钮的选中状态
        currentBtn?.selected = false
        
        //需要将前一个按钮的选中状态取消
        currentBtn = btn
        btn.selected = true
        
    }
}
